package com.example.group.network.express_plus_app.viewmodel;

import android.content.Context;
import android.util.Patterns;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.group.network.express_plus_app.R;
import com.example.group.network.express_plus_app.bean.MyResponse;
import com.example.group.network.express_plus_app.data.RegisterDataSource;
import com.example.group.network.express_plus_app.viewmodel.bean.RegisterFormState;

/**
 * @author 夏宁
 * @email 1941512303@qq.com
 * @date 2021/3/27 12:24
 */
public class RegisterViewModel extends ViewModel implements RegisterDataSource.RegisterResponseListener {
    Context context;
    private MutableLiveData<RegisterFormState> registerFormState = new MutableLiveData<>();
    private MutableLiveData<MyResponse> responseLD = new MutableLiveData<>();
    private RegisterDataSource registerDataSource;

    public RegisterViewModel(RegisterDataSource registerDataSource) {
        this.registerDataSource = registerDataSource;
        registerDataSource.setRegisterResponseListener(this);
    }
    public LiveData<RegisterFormState> getRegisterFormState() {
        return registerFormState;
    }

    public LiveData<MyResponse> getRegisterResult() {
        return responseLD;
    }
    //这里并没有返回值，所以设返回值也没用
    public void register(String phone, String password, Context applicationContext) {
        // can be launched in a separate asynchronous job
        context=applicationContext;
        registerDataSource.register(phone, password,applicationContext);
    }


    public void registerDataChanged(String phone, String password,String rePassword) {
        if (!isUserNameValid(phone)) {
            registerFormState.setValue(new RegisterFormState(R.string.invalid_username,null, null));
        } else if (!isPasswordValid(password)) {
            registerFormState.setValue(new RegisterFormState(null, R.string.invalid_password,null));
        } else if (!isRePasswordValid(password,rePassword)){
            registerFormState.setValue(new RegisterFormState(null,null,R.string.invalid_re_password));
        }else {
            registerFormState.setValue(new RegisterFormState(true));
        }
    }

    // A placeholder username validation check
    private boolean isUserNameValid(String username) {
        if (username == null) {
            return false;
        }
        if (username.contains("@")) {
            return Patterns.EMAIL_ADDRESS.matcher(username).matches();
        } else {
            return !username.trim().isEmpty();
        }
    }
    // A placeholder password validation check
    private boolean isPasswordValid(String password) {
        return password != null && password.trim().length() > 5;
    }
    private boolean isRePasswordValid(String password,String rePassword){
        if (password.equals(rePassword)){
            return true;
        }else {
            return false;
        }
    }
    //网络请求回调函数
    @Override
    public void setRegisterResponse(MyResponse myResponse) {
        responseLD.setValue(myResponse);
    }
}
